//Andrew Villadsen

import javax.vecmath.Point3d;
import javax.vecmath.Tuple3d;
import javax.vecmath.Vector3d;


//Class UserMovementRoutine monitors the user input listeners for commands.
public class UserMovementRoutine implements MovementRoutine
{
	UserMouseInputListener mouseListener;
	UserKeyboardInputListener keyboardListener;
	Disc heldDisc;
	private double speed;
	private Player user;
	private static final double dtheta = 0.1;
	private Field field;
	
	public UserMovementRoutine(UserMouseInputListener m, UserKeyboardInputListener k)
	{
		mouseListener = m;
		keyboardListener = k;
	}
	
	//Sets the user
	public void setUser(Player u)
	{
		user = u;
	}
	
	//Implements step from Actor.
	public Tuple3d[] step()
	{
		Point3d p = (Point3d) user.position.clone();
		Vector3d v = (Vector3d) user.velocity.clone();
		Tuple3d[] returnvalue = {p, v};
		
		if(heldDisc == null) //Player is (potentially) in motion
		{
			double theta = v.angle(new Vector3d(1.0, 0.0, v.z));
						
			if(keyboardListener.getTurnDirection() == 1)
				v = new Vector3d(speed * Math.cos(theta + dtheta), speed * Math.sin(theta + dtheta), v.z);
			else if(keyboardListener.getTurnDirection() == -1)
				v = new Vector3d(speed * Math.cos(theta - dtheta), speed * Math.sin(theta - dtheta), v.z);
			
			if(keyboardListener.playerIsMoving())
			{
				Vector3d dv = (Vector3d) v.clone();
				dv.scale(Field.dt);
				p.add(dv);
			}
		}
		return returnvalue;
	}
	
	//Sets the field.
	public void setField(Field gameField){
		field = gameField;
	}//End setField()
	
	//Sets the disc the user is holding to d.
	public void caughtDisc(Disc d)
	{
		heldDisc = d;
	}
}